<?php

class db_query {

    var $result;
    var $links;
    var $time_slow_log = 0.5;
    var $query;

    function db_query($query = null, $file_line_query = "") {
        if ($query == null)
            throw new Exception("Query is empty", 500);
        $this->query = $query;
        $dbinit = new db_init();


        if (@$_SERVER["SERVER_ADDR"] == "127.0.0.1") {
            $dbinit->getDeverloperServer();
        }

        $this->links = @mysql_pconnect($dbinit->server, $dbinit->username, $dbinit->passworddb);


        //Kết nối database
        if (!$this->links) {
            $str = "IP:" . $_SERVER['REMOTE_ADDR']
                    . " Not connect DB: host: " . $dbinit->server
                    . ", User : " . $dbinit->username
                    . " error: " . @mysql_error();
            save_logs("sql_error.log", $str);
            $response = new RequestResponse(500, "Cannot connect to database!!!");
            $response->process();
        }

        //Select database
        if (!$db_select = @mysql_select_db($dbinit->database, $this->links)) {
            save_logs("sql_error.log", "Cannot select database: " . $dbinit->database);
            $response = new RequestResponse(500, "Cannot select database!!!");
            $response->process();
        }

        @mysql_query("SET NAMES 'utf8'");

        $time_start = $this->microtime_float();

        $this->result = @mysql_query($query, $this->links);

        $time_end = $this->microtime_float();
        $time = $time_end - $time_start;

        if ($time >= $this->time_slow_log) {
            $str = "File : " . $file_line_query . "\n";
            $str .= "Query time : " . number_format($time, 10, ".", ",") . "\n";
            $str .= "IP:" . $_SERVER['REMOTE_ADDR'] . $query . chr(13);
            save_logs("sql_query_slow.log", $str);
        }


        if (!$this->result) {
            $error = @mysql_error($this->links);
            @mysql_close($this->links);
            save_logs("sql_error.log", $error . "\n" . $query);
            die($error . ": " . $query);
        }

        $dbinit->debug_query($query, $file_line_query);
        unset($dbinit);
    }

    function result_array($field_id = "") {
        $arrayReturn = array();
        while ($row = mysql_fetch_assoc($this->result)) {
            if ($field_id != "") {
                $arrayReturn[$row[$field_id]] = $row;
            } else {
                $arrayReturn[] = $row;
            }
        }
        return $arrayReturn;
    }

    function close() {
        @mysql_free_result($this->result);
        if ($this->links) {
            @mysql_close($this->links);
        }
    }

    function microtime_float() {
        list($usec, $sec) = explode(" ", microtime());
        return ((float) $usec + (float) $sec);
    }

}

class db_execute {

    var $links;
    var $total = 0;
    var $query;

    function db_execute($query, $file_line_query = "") {
        $this->query = $query;
        $dbinit = new db_init();

        if (@$_SERVER["SERVER_ADDR"] == "127.0.0.1") {
            $dbinit->getDeverloperServer();
        }

        $this->links = @mysql_pconnect($dbinit->server, $dbinit->username, $dbinit->passworddb);
        if (!$this->links) {
            $str = "IP:" . $_SERVER['REMOTE_ADDR']
                    . " Not connect DB: host: " . $dbinit->server
                    . ", User : " . $dbinit->username
                    . " error: " . @mysql_error();
            save_logs("sql_error.log", $str);
            $response = new RequestResponse(500, "Cannot connect to database!!!");
            $response->process();
        }
        if (!$db_select = @mysql_select_db($dbinit->database, $this->links)) {
            save_logs("sql_error.log", "Cannot select database: " . $dbinit->database);
            $response = new RequestResponse(500, "Cannot connect select database!!!");
            $response->process();
        }


        @mysql_query("SET NAMES 'utf8'");
        @mysql_query($query);

        $this->total = @mysql_affected_rows();

        if ($this->total < 0 && strpos($query, "IGNORE") === false) {
            $error = @mysql_error($this->links);
            @mysql_close($this->links);
            save_logs("sql_error.log", $file_line_query . " " . $error . "\n" . $query);
        }

        @mysql_close($this->links);

        $dbinit->debug_query($query, $file_line_query);
        unset($dbinit);
    }

}

class db_count {

    var $total;

    function db_count($sql) {
        $db_ex = new db_query($sql);

        if ($row = mysql_fetch_assoc($db_ex->result)) {
            $this->total = intval($row["count"]);
        } else {
            $this->total = 0;
        }
        $db_ex->close();
        unset($db_ex);
        return $this->total;
    }

}

class db_execute_return {

    var $links;
    var $result;
    var $query;

    function db_execute($query, $file_line_query = "") {
        $this->query = $query;
        $dbinit = new db_init();
        if (@$_SERVER["SERVER_ADDR"] == "127.0.0.1") {
            $dbinit->getDeverloperServer();
        }
        $this->links = @mysql_pconnect($dbinit->server, $dbinit->username, $dbinit->passworddb);
        if (!$this->links) {
            $str = "IP:" . $_SERVER['REMOTE_ADDR']
                    . " Not connect DB: host: " . $dbinit->server
                    . ", User : " . $dbinit->username
                    . " error: " . @mysql_error();
            save_logs("sql_error.log", $str);
            $response = new RequestResponse(500, "Cannot connect to database!!!");
            $response->process();
        }
        if (!$db_select = @mysql_select_db($dbinit->database, $this->links)) {
            save_logs("sql_error.log", "Cannot select database: " . $dbinit->database);
            $response = new RequestResponse(500, "Cannot connect select database!!!");
            $response->process();
        }

        @mysql_query("SET NAMES 'utf8'");
        @mysql_query($query);

        $total = @mysql_affected_rows();

        if ($total < 0 && strpos($query, "IGNORE") === false) {
            $error = @mysql_error($this->links);
            @mysql_close($this->links);
            save_logs("sql_error.log", $file_line_query . " " . $error . "\n" . $query);
        }

        $last_id = 0;
        $this->result = @mysql_query("select LAST_INSERT_ID() as last_id", $this->links);

        if ($row = @mysql_fetch_array($this->result)) {
            $last_id = $row["last_id"];
        }

        @mysql_close($this->links);

        $dbinit->debug_query($query, $file_line_query);
        unset($dbinit);
        return $last_id;
    }

}

?>